package com.ly.mpayment.rest.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.persist.entity.BusinessType;
import com.ly.mpayment.persist.service.businesstype.IBusinessTypeService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import cn.jpush.api.utils.StringUtils;
import net.sf.json.JSONObject;
@Component
@Path("system/businesstype/")
public class BusinessTypeRest extends AbsRest{

	@Autowired
	IUserService userService;

	@Autowired
	IBusinessTypeService busService;

	@POST
	@Path("searchBusinesType")
	public String searchBusinesType(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request, userService, "start","pageSize","busname","busCode");
		if(result == null){
			Map<String, Object> parameters = MapUtil.getMap();
			JSONObject json = JsonUtils.converToJson(data);
			if(!StringUtils.isEmpty(json.getString("busname"))){
				parameters.put(":busname", json.getString("busname").concat("%"));
			}
			if(!StringUtils.isEmpty(json.getString("busCode"))){
				parameters.put(":busCode", json.getString("busCode").concat("%"));
			}
			return ResultMessage.getSucceed(JsonUtils.excludes(this.busService.findPageBusiness(parameters, json.getInt("start"), json.getInt("pageSize")),
					new String[]{}));
		}
		return result;
	}

	@POST
	@Path("addBussinesType")
	public String addBussinesType(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "busCode","busname");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			if(!StringUtils.isEmpty(json.getString("busCode")) && !StringUtils.isEmpty(json.getString("busname"))){
				BusinessType type = null;
				type = new BusinessType();
				//检查名称和code是否重复
				if(this.busService.findByCode(json.getString("busCode")) == null){
					if(this.busService.findByCode(json.getString("busname")) == null){
						type.setBusCode(json.getString("busCode"));
						type.setBusname(json.getString("busname"));
						type.setRemark(json.containsKey("remark") ? json.getString("remark") : "");
						type.setStatus(Config.publicStatus[1]+"");
						if(this.busService.addBusinesstype(type)){
							return ResultMessage.getSucceed(null);
						}
						return ResultMessage.getFailt("");
					}
					return ResultMessage.getFailt(ResultMessage.busnameError);
				}
				return ResultMessage.getFailt(ResultMessage.buscodeError);
			}
		}
		return result;
	}

	@POST
	@Path("updateBussinesType")
	public String updateBussinesType(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data, request, userService, "status","busCode");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			if(!StringUtils.isEmpty(json.getString("busCode")) && !StringUtils.isEmpty(json.getString("busname"))){
				BusinessType type = this.busService.findByCode(json.getString("busCode"));
				if( type != null){
					type.setStatus(json.getString("status"));
					this.busService.updateBusiness(type);
					return ResultMessage.getSucceed(null);
				}
				return ResultMessage.getFailt(ResultMessage.buscodeError);
			}
		}
		return result;
	}
	@POST
	@Path("findAllBus")
	public String findAllBus(@Context HttpServletRequest request){
		return ResultMessage.getSucceed(JsonUtils.jsonArrayToJson(this.busService.findAllBus(),
				new String[]{"createtime","status","remark","operators"}));
	}
}
